package com.foreknow.elm.controller;

import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.service.impl.DeliveryAddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressController {

    /**
     * 根据用户编号查询所属送货地址
     * @param req
     * @return
     * @throws Exception
     */
    public List<Deliveryaddress> listDeliveryAddressByUserId(HttpServletRequest req) throws Exception{
        String userId = req.getParameter("userId");
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        List<Deliveryaddress> list = service.listDeliveryAddressByUserId(userId);
        return list;
    }

    /**
     * 根据送货地址编号查询送货地址
     * @param req
     * @return
     * @throws Exception
     */
    public Deliveryaddress getDeliveryAddressById(HttpServletRequest req) throws Exception{
        int daId = Integer.parseInt(req.getParameter("daId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        Deliveryaddress deliveryaddress = service.getDeliveryAddressById(daId);
        return deliveryaddress;
    }

    /**
     * 向送货地址表中添加一条记录
     * @param req
     * @return
     * @throws Exception
     */
    public int saveDeliveryAddress(HttpServletRequest req) throws Exception{
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        deliveryaddress.setContactName(req.getParameter("contactName"));
        try {
            deliveryaddress.setContactSex(Integer.parseInt(req.getParameter("contactSex")));
        } catch (Exception e){
            deliveryaddress.setContactSex(0);
        }
        deliveryaddress.setContactTel(req.getParameter("contactTel"));
        deliveryaddress.setAddress(req.getParameter("address"));
        deliveryaddress.setUserId(req.getParameter("userId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.saveDeliveryAddress(deliveryaddress);
        return result;
    }

    /**
     * 根据送货地址编号更新送货地址信息
     * @param req
     * @return
     * @throws Exception
     */
    public int updateDeliveryAddress(HttpServletRequest req) throws Exception{
        Deliveryaddress deliveryaddress = new Deliveryaddress();
        try {
            deliveryaddress.setDaId(Integer.parseInt(req.getParameter("daId")));
        } catch (Exception e){
            deliveryaddress.setDaId(0);
        }
        deliveryaddress.setContactName(req.getParameter("contactName"));
        try {
            deliveryaddress.setContactSex(Integer.parseInt(req.getParameter("contactSex")));
        } catch (Exception e){
            deliveryaddress.setContactSex(0);
        }
        deliveryaddress.setContactTel(req.getParameter("contactTel"));
        deliveryaddress.setAddress(req.getParameter("address"));
        deliveryaddress.setUserId(req.getParameter("userId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.updateDeliveryAddress(deliveryaddress);
        return result;
    }

    /**
     * 根据送货地址编号删除一条记录
     * @param req
     * @return
     * @throws Exception
     */
    public int removeDeliveryAddress(HttpServletRequest req) throws Exception{
        int daId;
        try {
            daId = Integer.parseInt(req.getParameter("daId"));
        } catch (Exception e){
            daId = 0;
        }
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        int result = service.removeDeliveryAddress(daId);
        return result;
    }
}
